/**
 * Created by mhy on 2018/6/30.
 */
import * as API from '../../api'
import * as types from '../mutation-types'

const login = {
  state: {
    userphone: '',
    code: '',
    coderesult: null,
    loginresult: null,
    userinforesult: null,
    infowx: null
  },
  mutations: {
    [types.GET_MOBILE_CODE] (state,data){
      state.result  = data
    },
    [types.LOGIN_PHONE] (state,data) {
      state.loginresult = data
    },
    [types.USER_INFO] (state,data){
      state.userinforesult = data
    },
    [types.LOGIN_WX] (state,data){
      state.loginresult = data
    },
    [types.INFO_WX] (state,data){
      state.infowx = data
    }
  },
  getters: {

  },
  actions: {
    //获得验证码
    getMobileCode ({commit,state,rootState},{userphone}) {
      var prom = API.getMobileCode(userphone)
      prom.then(function (data) {
        state.coderesult = data
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    //微信登录
    loginWithWX({commit,state,rootState},params) {
      var prom = API.loginWx(params)
      prom.then(function (data) {
        commit(types.LOGIN_WX, data.result)
        localStorage.setItem('token', data.result.token)
        localStorage.setItem('userid', data.result.userid)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    //微信信息查询
    infoWithWX({commit,state,rootState},params) {
      var prom = API.wxinfo(params)
      prom.then(function (data) {
        commit(types.LOGIN_WX, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    //登录
    loginWithPhone({commit,state,rootState},{userphone,code,codeid}) {
      var prom = API.loginWithPhone(userphone,code,codeid)
      prom.then(function (data) {
        commit(types.LOGIN_PHONE, data.result)
        localStorage.setItem('token', data.result.token)
        localStorage.setItem('userid', data.result.userid)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    //用户信息
    getUserInfo({commit,state,rootState},{userid}){
      var prom = API.getUserInfo(userid)
      prom.then(function (data) {
        commit(types.USER_INFO, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    }
    //
    //
    //
    //
  }
}

export default login
